﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Workflows.Components.TextMining.Clustering
{
    /// <summary>
    /// it measures the disorder in a set. 
    /// </summary>
    public class EntropyPurityCalculator:IPurityCalculator
    {
        #region IPurityCalculator Members
        /// <summary>
        /// 0 = pure
        /// </summary>
        /// <param name="termFrequencies"></param>
        /// <returns>[0,1) where 0=pure</returns>
        public double CalculatePurityScore(Dictionary<int, int> termFrequencies)
        {
            int total = 0;
            foreach (int termID in termFrequencies.Keys)
            {
                total += termFrequencies[termID];
            }
            double score = 0;
            foreach(int termID in termFrequencies.Keys)
            {
                double relativeFreq = (double) termFrequencies[termID]/total;
                score -= relativeFreq*Math.Log(relativeFreq, Math.E);
            }
            return score;
        }

        #endregion
    }
}
